<template>
	<view>
		<template v-if="isUnauthorized">
			<unauthorized></unauthorized>
		</template>
		<template v-if="tabs.length>0">
			<u-sticky bgColor="#fff">
				<!-- <view class="blank20"></view>
			<view class="flex wrapper">
				<view class="flex-1 font-bold">
					【{{date}}】 {{cost_center_id_name}}
				</view>
			</view>
			<view class="blank20"></view>
			<u-line></u-line> -->
				<u-tabs :list="tabs" @click="tabClick" :activeStyle="activeStyle" lineColor="#675f91"
					:current="current">
					<view slot="right" style="padding-right: 10px;">
						<view class="flex">
							<!-- 销售 -->
							<view v-show="tabs[current].name=='销售明细' && sales_list.length > 0">
							<u-button type="info" size="small" :text="saleOpen?'全部收起':'全部展开'" @tap="toggleAll"></u-button>
							</view>
							<view class="ml15" v-show="tabs[current].name=='销售明细' "
								v-if="cus_list.length>0 &&!sales_isTimeout">
								<picker @change="bindAddCus" value="0" :range="cus_list" range-key="label">
									<u-button type="primary" size="small" text="添加"></u-button>
								</picker>
							</view>
							<!-- 销售 end-->
							<!-- 暂隐藏 -->
							<!-- <view v-show="tabs[current].name=='成本明细' " v-if="cost_cate_list.length > 0">
								<picker @change="bindAddCost" value="0" :range="cost_cate_list" range-key="label">
									<u-button type="primary" size="small" text="添加"></u-button>
								</picker>
							</view> -->
						</view>
					</view>
				</u-tabs>
				<u-line></u-line>
				<template v-if="tabs[current].name=='成本明细'">
					<view class="item bg-gray">
						<view class="flex font-24 text-light-gray">
							<view style="width: 16%;" v-if="costTableHd.cost_category_id">
								类别
							</view>
							<view class="flex-1 text-center ml15 mr15" v-if="costTableHd.cost_amount">
								金额
							</view>
							<view class="text-center" style="width: 20%;" v-if="costTableHd.cost_ratio">
								日比例
							</view>
							<view class="text-center" style="width: 20%;" v-if="costTableHd.month_cost_ratio">
								月比例
							</view>
							<view class="text-center" style="width: 20px;">
								
							</view>
						</view>
					</view>
					<u-line></u-line>
				</template>
			</u-sticky>
			<view class="" v-show="tabs[current].name=='销售明细'">
				<nodata v-if="loadingStatus == 'end' && sales_list.length == 0"></nodata>
				<template v-for="(v,i) in sales_list">
					<!-- {{v.open?'1':'0'}} -->
					<saleItem :detail="v" :date="date" :open="v.open" :readonly="v.readonly" @add="addSale"
						:modelname="sales_model_name" :dinners="dinner_type_category_list" :sales="sales_category_list" @reset="resetSaleItem" @updata="updataSaleItem"
						@openDivision="openDivision" :cfg="cus_sales_obj[v.cus_loc_id]"
						:division="division_obj[v.cus_loc_id]||[]"></saleItem>
				</template>

			</view>
			<view class="" v-show="tabs[current].name=='成本明细'">
				<nodata v-if="loadingStatus == 'end' && cost_list.length == 0"></nodata>
				<template v-for="(v,i) in cost_list">
					<!-- :modelname="cost_model_name" -->
					<costItem :detail="v" :readonly="v.readonly" 
					:date="date" :table_hd="costTableHd"
						:month_sales_amount="month_sales_amount" :day_sales_amount="day_sales_amount" @del="delCost" @add="addCostRecord" @openCostRecord="openCostRecord" @updata="updataCostItem">
					</costItem>
				</template>
			</view>

			<view class="blank"></view>
			<cover-view class="fixed-bottom bgw" v-show="tabs[current].name=='成本明细'">
				<cover-view class="item">
					<cover-view class="flex">
						<cover-view class="text-light-gray" style="width: 16%;" v-if="costTableHd.cost_category_id">
							汇总
						</cover-view>
						<cover-view class="flex-1 font-bold text-center ml15 mr15" v-if="costTableHd.cost_amount">
							￥{{sum_cost_amount}}
						</cover-view>
						<cover-view class="text-center font-bold" style="width: 20%;" v-if="costTableHd.cost_ratio">
							{{sum_cost_ratio}}
						</cover-view>
						<cover-view class="text-center font-bold" style="width: 20%;" v-if="costTableHd.month_cost_ratio">
							{{sum_month_cost_ratio}}
						</cover-view>
						<cover-view class="text-center" style="width: 20px;">

						</cover-view>
					</cover-view>
				</cover-view>
			</cover-view>
			<cover-view class="fixed-bottom bgw" v-show="tabs[current].name=='销售明细'">
				<cover-view class="item">
					<cover-view class="flex" style="justify-content: space-between;">
						<cover-view class="text-light-gray">
							汇总
						</cover-view>
						<cover-view class="flex">
							<cover-view class="text-gray">份数：</cover-view>
							<cover-view class="font-bold ">{{day_sales_qty}}</cover-view>
						</cover-view>
						<cover-view class="flex">
							<cover-view class="text-gray">金额：</cover-view>
							<cover-view class="font-bold ">￥{{day_sales_amount}}</cover-view>
						</cover-view>
					</cover-view>
				</cover-view>
			</cover-view>
		</template>

		<u-popup :show="popupShow" @close="popupClose" @open="popupOpen" mode="top">
			<division :title="divisionData.title" :list="divisionData.list" :readonly="divisionData.readonly"
				@confirm="divisionConfirm" @cancel="divisionCancel" v-if="popupShow"></division>
		</u-popup>

		<u-popup :show="costRecordShow" @close="costRecordPopupClose" @open="costRecordPopupOpen" mode="top">
			<costRecord :title="costRecordData.title" :list="costRecordData.list" :readonly="costRecordData.readonly" :category="costRecordData.category" :classfication="costRecordData.classfication" :date="date"
				@confirm="costRecordConfirm" @cancel="costRecordCancel" v-if="costRecordShow" @add="addCostRecord"></costRecord>
		</u-popup>

	</view>
</template>

<script>
	import {
		one_day_kpi_report_detail,
		save_one_day_sales_cost_detail,
		common_write
	} from '@/common/api.js';
	import saleItem from './components/sale-item.vue';
	import division from './components/division.vue';
	
	const _nowTime = new Date().getTime();

	import costItem from './components/cost-item.vue';
	import costRecord from './components/cost-record.vue';
	import unauthorized from '@/components/unauthorized.vue';
	import nodata from '@/components/nodata.vue';

	export default {
		components: {
			saleItem,
			division,
			costItem,
			costRecord,
			unauthorized,
			nodata,
		},
		data() {
			return {
				current: 0,
				activeStyle: {
					color: '#675f91'
				},
				tabs: [],

				list: [],
				isBoss: false,
				date: '',
				cost_center_id: '',
				cost_center_id_name: '',
				cus_group_id_name:'',
				cus_group_id:'',
				add_sales_record_id: this.make_add_function(),
				add_cost_record_id: this.make_add_function(),
				cost_list: [],
				sales_list: [],
				sales_data: [],
				sales_model_name: '',
				cost_model_name: '',
				cost_month_cost_dic:{},
				saleOpen: false,
				saleWriteCus: [],
				saleReadCus: [],
				sales_deadline_alarm_time_str: '',
				dinner_type_category_list: [],
				sales_category_list: [],
				cost_category_list: [],
				cost_category_obj:{},
				cus_sales_obj: {},
				sales_isTimeout: false,
				costWrite: [],
				costRead: [],
				isUnauthorized: false, //无权限 成本和销售均无权限
				month_sales_amount: 0,
				day_sales_amount: 0,
				day_sales_qty:0,
				loadingStatus: '',
				division_obj: {},
				// 部门
				popupShow: false,
				divisionData: {
					title: '',
					readonly: false,
					list: [],
				},
				divisionFn: null,
				// 成本记录
				costRecordShow: false,
				costRecordData: {
					title: '',
					readonly: false,
					list: [],
				},
				costRecordFn: null,
				
				default_data:{},
				timerOut: null,//超时定时器
				costTableHd:{},
				
				sum_month_cost_ratio:0,
				sum_cost_ratio:0,
				sum_cost_amount:0,
				
				cus_status_dic:{},
				
				isUpdata:false // 数据发生修改
			}
		},
		computed: {
			cus_list() {
				let cus_list = [];
				this.saleWriteCus.forEach(v => {
					let cus = this.sales_list.find(s => s.cus_loc_id == v[0]);
					let is_cus_status = this.is_cus_status(v[0]);
					if (!cus && is_cus_status) {
						cus_list.push({
							id: v[0],
							label: v[1]
						})
					}
				})
				return cus_list;
			},
			cost_cate_list() {
				let cost_cate_list = [];
				this.costWrite.forEach(v => {
					let cost = this.cost_list.find(s => s.cost_category_id[0] == v[0]);
					if (!cost) {
						cost_cate_list.push({
							id: v[0],
							label: v[1]
						})
					}
				})
				return cost_cate_list;
			},
		},
		methods: {
			// 成本记录↓
			costRecordConfirm(data) {
				// console.log(data);
				this.costRecordFn(data);
				this.costRecordShow = false;
			},
			costRecordCancel() {
				this.costRecordShow = false;
			},
			openCostRecord(data, fn) {
				this.costRecordData = data;
				this.costRecordFn = fn;
				this.costRecordShow = true;
			},
			costRecordPopupClose() {

			},
			costRecordPopupOpen() {

			},
			//成本记录↑
			// 销售部门↓
			// 客户是否过期 过期则不可编辑
			is_cus_status(cusId){
				if(!this.cus_status_dic[cusId]){
					return true;
				}
				if(this.cus_status_dic[cusId][0] != 'stop'){
					return true;
				}
				let stopDateTime = new Date(this.cus_status_dic[cusId][1]).getTime();
				if(stopDateTime!=0 && stopDateTime<_nowTime){
					return false;
				}
				return true;
			},
			divisionConfirm(data) {
				console.log(data);
				this.divisionFn(data);
				this.popupShow = false;
			},
			divisionCancel() {
				this.popupShow = false;
			},
			openDivision(data, fn) {
				this.divisionData = data;
				this.divisionFn = fn;
				this.popupShow = true;
			},
			popupClose() {

			},
			popupOpen() {

			},
			// 销售部门↑		
			pageScrollToBottom() {
				setTimeout(() => {
					uni.pageScrollTo({
						scrollTop: 9999999,
						duration: 300
					});
				}, 100)
			},
			delCost(data) {
				this.cost_list = this.cost_list.filter(v => v.id != data.id);
			},
			bindAddCost(e) {
				let index = e.detail.value;
				let cost = this.cost_cate_list[index];
				let data = {
					cost_category_id: [cost.id, cost.label]
				};
				this.addCost(data);
				this.cost_list.push(data);
				this.pageScrollToBottom();
			},
			bindAddCus(e) {
				let index = e.detail.value;
				let cus = this.cus_list[index];
				// console.log(cus);
				let data = {
					cus_loc_id: [cus.id, cus.label]
				};
				this.addSale(data);
				this.sales_list.push({
					cus_loc_id: cus.id,
					open: true,
					childs: [data],
					readonly: false
				})
				this.pageScrollToBottom();
			},
			computed_sum_month_cost_ratio() {
				let month_sales_amount = this.month_sales_amount || 0;
				if (!month_sales_amount) {
					return '0%';
				}
				let num = 0;
				this.cost_list.forEach(v => {
					num += v.cost_amount;
					num += v.month_cost_other_days;
				})
				let result = this.formatPrice(num / month_sales_amount * 100);
				return result + '%';
			},
			computed_sum_cost_ratio() {
				let day_sales_amount = this.day_sales_amount || 0;
				if (!day_sales_amount) {
					return '0%';
				}
				let num = 0;
				this.cost_list.forEach(v => {
					num += v.cost_amount;
				})
				let result = this.formatPrice(num / day_sales_amount * 100);
				return result + '%';
			},
			computed_sum_cost_amount() {
				let num = 0;
				this.cost_list.forEach(v => {
					num += v.cost_amount;
				})
				return this.formatPrice(num);
			},
			updataCostSum(){
				this.sum_month_cost_ratio = this.computed_sum_month_cost_ratio();
				this.sum_cost_ratio = this.computed_sum_cost_ratio();
				this.sum_cost_amount = this.computed_sum_cost_amount();
			},
			updataCostItem(data){
				// #ifdef MP-WEIXIN
				let index = this.cost_list.findIndex(v=>v.id == data.id);
				this.cost_list[index] = data;
				// #endif
				this.updataCostSum();
				this.isUpdata = true;
			},
			updataSaleItem(data,newId,diff,diffQty) {
				// console.log(diffQty);
				if(data){
					let index = this.sales_data.findIndex(v => v.id == data.id);
					if (newId) {
						data.id = newId;
					}
					this.sales_data[index] = data;
				}else{
					//删除
				}
				this.day_sales_amount = this.day_sales_amount + diff;
				this.day_sales_qty = this.day_sales_qty + diffQty;
				this.month_sales_amount = this.month_sales_amount + diff;
				this.updataCostSum();
				this.isUpdata = true;
			},
			resetSaleItem(data) {
				let index = this.sales_data.findIndex(v => v.id == data.id);
				data = this.sales_data[index];
			},
			toggleAll() {
				this.saleOpen = !this.saleOpen;
				this.sales_list.forEach(v => {
					v.open = this.saleOpen;
				})
			},
			tabClick(e) {
				this.current = e.index;
				// console.log(this.list);
				// this.$forceUpdate();
			},
			addCost(data) {
				let item = JSON.parse(JSON.stringify(data));
				let cost_category_id = data.cost_category_id[0]
				data.id = cost_category_id;
				data.cost_center_id = [this.cost_center_id, this.cost_center_id_name];
				let cate_cost_classfication = this.cost_category_obj[cost_category_id].cost_classfication;
				data.cate_cost_classfication = cate_cost_classfication;
				data.date = this.date;
				data.cost_amount = '';
				data.cost_ratio = 0;
				data.month_cost_ratio = 0;
				data.note = false;
				data.month_cost = this.cost_month_cost_dic[cost_category_id] || 0;
				data.month_cost_other_days = data.month_cost;//其他日期的月成本总
				
				if(cate_cost_classfication == 'am'){
					item.cost_classfication = 'am';
				}
				if(cate_cost_classfication == 'dp'){
					item.cost_classfication = 'dp';
				}
				this.addCostRecord(item);
				data.list = [item];
			},
			addCostRecord(data){
				let cost_classfication = data.cost_classfication || 'manual_input';
				let cost_obj_id = this.default_data.default_cost_obj_id;
				let rel_partner_id = this.default_data.default_rel_partner_id;
				data.id = this.add_cost_record_id();
				data.cost_center_id = [this.cost_center_id, this.cost_center_id_name];
				data.date = this.date;
				data.amdp_amount = 0;
				data.ap_amount = 0;
				data.cash_amount = 0;
				data.cost_amount = '';
				data.cost_classfication = cost_classfication;
				data.cost_obj_id = cost_obj_id;
				data.cost_ratio = 0;
				data.month_cost_ratio = 0;
				data.note = false;
				data.rel_partner_id = rel_partner_id;
				data.res_id = '';
				data.res_model = '';
			},
			addSale(data) {
				data.dinner_type = '';
				data.dinner_type_id = []; //[34, "中餐"]
				data.division_qty_json = false;
				data.price = '';
				data.sales_category_id = []; //[2, "中餐"]
				data.sales_qty = '';
				data.id = this.add_sales_record_id();
				this.sales_data.push(data);
			},
			// 暂不用
			saveSale(item) {
				console.log(item);
				this.saleLoading[item.id] = true;
				console.log(this.saleLoading);
				common_write({
					model_name: 'sales_record',
					vals_list: [item]
				}).then((res) => {
					setTimeout(() => {
						this.saleLoading[item.id] = false;
					}, 2000)
				}).catch(() => {

				}).finally(() => {

				})
			},
			//弃用
			checkTime(data) {
				let isTimeout = this.isTimeout();
				this.sales_isTimeout = isTimeout;
				if (isTimeout) {
					data && (data.value = false);
					this.$store.dispatch('modal', {
						content: '已超时，【' + this.date + '】的销售数据只能查看，不可编辑。',
						showCancel: false
					}).then(() => {
						if (data) {
							this.getData()
						}
					})
				} else {
					data && (data.value = true);
				}
			},
			// 是否超时 弃用
			isTimeout() {
				return false;
				
				let deadline_alarm_time_str = this.sales_deadline_alarm_time_str;
				if (!deadline_alarm_time_str) {
					return false;
				}
				let today = new Date();
				let nowTime = today.getTime()
				let today_year_month_day = `${today.getFullYear()}` + `-` + `${today.getMonth()+1}`.padStart(2, '0') +
					`-` + `${today.getDate()}`.padStart(2, '0');

				let yesterday = new Date(nowTime - (24 * 60 * 60 * 1000 * 1)); //昨天
				let yesterday_year_month_day = `${yesterday.getFullYear()}` + `-` + `${yesterday.getMonth()+1}`.padStart(2,
					'0') + `-` + `${yesterday.getDate()}`.padStart(2, '0');
				let yesterdayTime = new Date(yesterday_year_month_day).getTime();

				let currTime = new Date(this.date).getTime();
				if (currTime < yesterdayTime) {
					//昨天以前
					return true;
				}
				if (currTime == yesterdayTime) {
					let endTime = new Date(today_year_month_day + ' ' + deadline_alarm_time_str).getTime();
					if (nowTime > endTime) {
						return true;
					}
				}
				return false;
			},
			set_timerOut(){
				console.log('set_timerOut');
				let that = this;
				if(!that.sales_deadline_alarm_time_str){
					return;
				}
				let nowTime = new Date().getTime();
				let deadlineTime = new Date(that.sales_deadline_alarm_time_str).getTime();
				let time = deadlineTime - nowTime;
				if(time>0){
					that.sales_isTimeout = false;
					that.timerOut = setTimeout(()=>{
						that.$store.dispatch('modal', {
							content: '已超时，【' + that.date + '】的销售数据只能查看，不可编辑。',
							showCancel: false
						}).then(() => {
							if (data) {
								that.getData()
							}
						})
					},time)
				}else{
					that.sales_isTimeout = true;
				}
			},
			set_cus_sales_obj(cus_sales_cfg_list) {
				let cus_sales_obj = {};
				cus_sales_cfg_list.forEach(v => {
					let cus_loc_id = v.cus_loc_id[0];
					!cus_sales_obj[cus_loc_id] && (cus_sales_obj[cus_loc_id] = {});
					let dinner_type_id = v.dinner_type_id[0];
					!cus_sales_obj[cus_loc_id][dinner_type_id] && (cus_sales_obj[cus_loc_id][dinner_type_id] = {});
					let sales_category_id = v.sales_category_id[0];
					!cus_sales_obj[cus_loc_id][dinner_type_id][sales_category_id] && (cus_sales_obj[cus_loc_id][
						dinner_type_id
					][sales_category_id] = {});
					cus_sales_obj[cus_loc_id][dinner_type_id][sales_category_id] = v;
				})

				this.cus_sales_obj = cus_sales_obj;
			},
			set_cost_list(data) {
				let cost_list = [];
				let cost_obj = {};
				data.forEach(v => {
					let _readonly = false;
					let _isRead = true;
					let cost_category_id = v.cost_category_id[0];
					let cost_category_name = v.cost_category_id[1];
					// console.log(cost_category_id);
					let cate_cost_classfication = this.cost_category_obj[cost_category_id].cost_classfication;
					let cost = this.costRead.find(c => c[0] == cost_category_id);
					if (!cost) {
						_isRead = false;
					}
					if (_isRead) {
						let writeCost = this.costWrite.find(c => c[0] == cost_category_id);
						if (!cost_obj[cost_category_id]) {
							cost_obj[cost_category_id] = {
								id:cost_category_id,
								cate_cost_classfication:cate_cost_classfication,
								cost_category_id:[cost_category_id,cost_category_name],
								readonly: !writeCost,
								cost_amount: 0,
								cost_ratio:0,
								month_cost_ratio:0,
								cost_center_id:[this.cost_center_id, this.cost_center_id_name],
								month_cost:this.cost_month_cost_dic[cost_category_id] || 0,
								month_cost_other_days:0,//其他日期的月成本总
								list: []
							}
						}
						if (v.cost_classfication == 'am' || v.cost_classfication == 'dp') {
							cost_obj[cost_category_id].cost_amount += v.amdp_amount||0;
						} else if (v.cost_classfication == 'stock_out') {
							cost_obj[cost_category_id].cost_amount += v.booked_cost_amount||0;
						}else if(v.cost_classfication == 'manual_input'){
							cost_obj[cost_category_id].cost_amount += v.cost_amount||0;
						}
						if(v.id == 0){
							v.id = this.add_cost_record_id()
						}
						v.cost_center_id = [this.cost_center_id, this.cost_center_id_name];
						cost_obj[cost_category_id].list.push(v);
					}
				})
				for(let cost_category_id in cost_obj){
					let item = cost_obj[cost_category_id];
					item.cost_amount = this.formatPrice(item.cost_amount);
					item.month_cost_other_days = item.month_cost - item.cost_amount;
					// item.month_cost_ratio = item.month_cost/this.month_sales_amount || 0;
					// item.cost_ratio = item.cost_amount/this.day_sales_amount || 0;
					cost_list.push(item);
				}
				cost_list = cost_list.sort((a, b) => a.cost_category_id[0] - b.cost_category_id[0]);
				console.log(cost_list);
				this.cost_list = cost_list;
				
				this.updataCostSum();
			},
			set_sales_list(data){
				let sales_list = [];
				let sales_obj = {};
				let day_sales_qty = 0;
				
				data.forEach(v => {
					if(v.id == 0){
						v.id = this.add_sales_record_id();
					}
					day_sales_qty+= v.sales_qty;
					!sales_obj[v.cus_loc_id[0]] && (sales_obj[v.cus_loc_id[0]] = []);
					sales_obj[v.cus_loc_id[0]].push(v);
				})
				this.sales_data = data;
				this.day_sales_qty = day_sales_qty;
				for (let cus_loc_id in sales_obj) {
					let _readonly = false;
					if (this.sales_isTimeout) {
						_readonly = true;
					}
					let _isRead = true;
					let cus = this.saleReadCus.find(v => v[0] == cus_loc_id);
					if (!cus) {
						_isRead = false;
					}
					let cus2 = this.saleWriteCus.find(v => v[0] == cus_loc_id);
					!cus2 && (_readonly = true);
					if (_isRead) {
						if(!_readonly){
							_readonly = !this.is_cus_status(cus_loc_id);
						}
						sales_list.push({
							cus_loc_id: cus_loc_id,
							childs: sales_obj[cus_loc_id],
							readonly: _readonly
							// readonly: true
						})
					}
				}
				if (sales_list.length == 1) {
					this.saleOpen = true;
				}
				sales_list.forEach(v => {
					v.open = this.saleOpen;
					// v.open = true;
				})
				this.sales_list = sales_list;
			},
			getData() {
				if(this.timerOut) {
					clearInterval(this.timerOut);  
					this.timerOut = null;
				}
				uni.showLoading({
					title: '加载中'
				});
				this.loadingStatus = 'loading';
				one_day_kpi_report_detail({
					date: this.date,
					cost_center_id: this.cost_center_id,
					cus_group_id:this.cus_group_id || false
				}).then((res) => {
					let {
						cost_data,
						division_list,
						sales_data,
						default_data,
						cost_category_data,
						cus_status_dic
					} = res.result;
					this.cus_status_dic = cus_status_dic;
					// 阅读权限
					if (cost_data.config.read.length == 0 && sales_data.config.read_list.length == 0) {
						this.isUnauthorized = true;
						return;
					} else {
						this.isUnauthorized = false;
					}
					this.default_data = default_data;
					let tabs = [];
					if (sales_data.config.read_list.length > 0) {
						tabs.push({
							name: '销售明细'
						})
					}
					if (cost_data.config.read.length > 0) {
						tabs.push({
							name: '成本明细'
						})
					}
					this.tabs = tabs;

					this.month_sales_amount = sales_data.month_sales_amount;
					this.day_sales_amount = sales_data.day_sales_amount;

					// 成本
					let costTableHd = {
						cost_category_id:false,
						cost_amount:false,
						cost_ratio:false,
						month_cost_ratio:false,
					}
					cost_data.config['字段列表'].forEach(k=>{
						costTableHd[k] = true;
					})
					this.costTableHd = costTableHd;
					
					this.cost_model_name = cost_data.config.model_name;
					this.cost_month_cost_dic = cost_data.config.month_cost_dic || {};
					this.costWrite = cost_data.config.write || [];
					this.costRead = cost_data.config.read || [];
					this.cost_category_list = cost_category_data;
					let cost_category_obj = {};
					cost_category_data.forEach(v=>{
						cost_category_obj[v.id] = v;
					})
					this.cost_category_obj = cost_category_obj;	
					this.set_cost_list(cost_data.data);

					// 销售
					this.saleWriteCus = sales_data.config.write_list || [];
					this.saleReadCus = sales_data.config.read_list || [];
					if (this.saleReadCus.length > 0) {
						this.dinner_type_category_list = sales_data.config.dinner_type_category_list;
						this.sales_category_list = sales_data.config.sales_category_list;
						this.sales_model_name = sales_data.config.model_name;
						this.sales_deadline_alarm_time_str = sales_data.config.deadline_alarm_time_str;
						this.set_cus_sales_obj(sales_data.config.cus_sales_cfg_list);
						if(this.saleWriteCus.length > 0){
							this.set_timerOut();//this.sales_isTimeout
						}
						let division_obj = {};
						division_list.forEach(v => {
							!division_obj[v.cus_loc_id[0]] && (division_obj[v.cus_loc_id[0]] = []);
							division_obj[v.cus_loc_id[0]].push(v);
						})
						this.division_obj = division_obj;
						
						this.set_sales_list(sales_data.data);
					}
				}).catch(() => {

				}).finally(() => {
					this.loadingStatus = 'end';
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
			}

		},
		onLoad(d) {
			// date=2023-11-22&cost_center_id=335
			this.date = d.date || '2024-04-08';
			this.cost_center_id = Number(d.cost_center_id) || 2924;
			this.cost_center_id_name = d.cost_center_id_name || '测试';
			this.cus_group_id_name = d.cus_group_id_name || '客户组测试';
			this.cus_group_id = Number(d.cus_group_id) || 24;
			try {
				this.role = this.$store.getters.get_role;
				this.userInfo = this.$store.getters.get_userInfo;
				this.isBoss = this.userInfo.cost_center_category_list.length > 1;

				this.isBoss = false;

				// console.log(this.role['报表模块']['成本']);
				// console.log(this.role['报表模块']['销售']);
				// console.log(this.userInfo);
				this.getData();
			} catch (e) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
			}
		},
		onShow() {


		},
		onUnload(){
			try{
				if(this.timerOut) { 
					console.log('注销定时器')
					clearInterval(this.timerOut);  
					this.timerOut = null;
				}
			}catch(e){
				
			}
			if(this.isUpdata){
				uni.$emit('refresh_report');
			}
		},
		onReady() {
			let navigationBarTitle = this.date + ' [' + this.cost_center_id_name + ']';
			if(this.cus_group_id_name){
				navigationBarTitle+= ' ['+this.cus_group_id_name+']';
			}
			
			uni.setNavigationBarTitle({
				title: navigationBarTitle
			});
		},
		onPullDownRefresh() {
			this.getData();
		}
	}
</script>

<style lang="scss" scoped>
	.blank {
		height: 100upx;
	}

	/deep/.item {
		padding: 20upx 20upx;
	}

	/deep/.food-box {
		margin: 0 20upx 0 20upx;
		background-color: #ededed;
		padding: 0;
	}

	/deep/.food-box .item {
		padding: 10upx 10upx;
	}

	/deep/.active.time-item-box {
		position: relative;
		// border: 1px solid #675f91;
		// border-top: 1px solid #675f91;
		border-bottom: 1px solid #675f91;
	}

	/deep/.time-box::before {
		// position: absolute;
		// content: ' ';
		// width: 2px;
		// height: 100%;
		// top: 0;
		// bottom: 0;
		// left: 2upx;
		// background-color: #675f91;
	}

	/deep/ .icon-btn {
		padding: 3px;
		border: 1px solid #d9d9d9;
		border-radius: 50%;
	}

	/deep/ .icon-btn.active {
		background-color: #675f91;
		border-color: #675f91;
	}

	/deep/.input_border_bottom {
		line-height: 72upx;
		height: 72upx;
		font-size: 28upx;
		border-bottom: 1px solid #cccccc;
	}

	/deep/.input_border_bottom_red {
		color: #D6251F;
		border-bottom: 1px solid #D6251F;
	}
	.fixed-bottom{
		border-top: 1px solid #cccccc;
		z-index: 9999;
	}
</style>